package cn.snailhd.util.test;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class NarrowImage {  
	  
    /** 
     * @param im 
     *            ԭʼͼ�� 
     * @param resizeTimes 
     *            ����,����0.5������Сһ��,0.98�ȵ�double���� 
     * @return ���ش�����ͼ�� 
     */  
    public BufferedImage zoomImage(String src) {  
          
        BufferedImage result = null;  
  
        try {  
            File srcfile = new File(src);  
            if (!srcfile.exists()) {  
                System.out.println("�ļ�������");  
                  
            }  
            BufferedImage im = ImageIO.read(srcfile);  
  
            /* ԭʼͼ��Ŀ�Ⱥ͸߶� */  
            int width = im.getWidth();  
            int height = im.getHeight();  
              
            //ѹ������  
            float resizeTimes = 0.3f;  /*���������Ҫת���ɵı���,�����1����ת����1��*/  
              
            /* ������ͼƬ�Ŀ�Ⱥ͸߶� */  
            int toWidth = (int) (width * resizeTimes);  
            int toHeight = (int) (height * resizeTimes);  
  
            /* ����ɽ��ͼƬ */  
            result = new BufferedImage(toWidth, toHeight,  
                    BufferedImage.TYPE_INT_RGB);  
  
            result.getGraphics().drawImage(  
                    im.getScaledInstance(toWidth, toHeight,  
                            java.awt.Image.SCALE_SMOOTH), 0, 0, null);  
              
  
        } catch (Exception e) {  
            System.out.println("��������ͼ�����쳣" + e.getMessage());  
        }  
          
        return result;  
  
    }  
      
     public boolean writeHighQuality(BufferedImage im, String fileFullPath) {  
            try {  
                /*������ļ���*/  
                FileOutputStream newimage = new FileOutputStream(fileFullPath);  
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);  
                JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(im);  
                /* ѹ������ */  
                jep.setQuality(0.95f, true);  
                encoder.encode(im, jep);  
               /*��JPEG����*/  
                newimage.close();  
                return true;  
            } catch (Exception e) {  
                return false;  
            }  
        }  
       
       
     public static void main(String[] args) {  
           
         String inputFoler = "D:\\Workspaces\\MyEclipse2015\\wallpape"
				+ "rSite\\src\\main\\webapp\\resources\\img\\c4b745124a_max.jpg" ;   
         /*�����д����Ҫ��СͼƬ���ļ���ȫ��ַ*/  
        String outputFolder = "D:\\Workspaces\\MyEclipse2015\\wallpape"
				+ "rSite\\src\\main\\webapp\\resources\\img\\c4b745124a2.jpg";    
        /*�����д��ת�����ͼƬ��ŵ��ļ���*/  
        
         
           
         NarrowImage narrowImage = new NarrowImage();  
         narrowImage.writeHighQuality(narrowImage.zoomImage(inputFoler), outputFolder);  
          
    }  
  
}